Vue.component('v-select', VueSelect.VueSelect);
let  url = location.href
let questionId = url.split("?")[1]
let editApp = new Vue({
    el:"#editApp",
    data:{
        question:{},
        tags:[],
        teachers:[],
        selectedTags:[],
        selectedTeachers:[]

    },
    methods:{
        getQuestion(){
            axios.get('/portal/question/editQuestion/'+questionId)
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code == OK){
                        this.question = r.data
                        $("#summernote").val(this.question.content)
                    }
                })
        },
        loadTags(){
            axios.get("/portal/tag/tags")
                .then(result => {
                    let r = result.data
                    if(r.code == OK){
                        for (let i = 0;i<r.data.length;i++)
                            this.tags.push(r.data[i].name)
                    }

                })
        },
        loadTeachers(){
            axios.get('/portal/user/teachers')
                .then(result=>{
                    let r = result.data
                    if(r.code == OK){
                        for (let i = 0; i < r.data.length; i++) {
                            this.teachers.push(r.data[i].nickname)
                        }
                    }
                })
        },
        updateQuestion(){
            let data ={
                title : this.question.title,
                content : $("#summernote").val(),
                selectedTags : this.question.selectedTags,
                selectedTeachers : this.question.selectedTeachers,
                questionId : questionId
            }
            console.log(data)
            axios.post('/portal/question/updateQuestion',data)
                .then(result=>{
                    let r = result.data
                    if(r.code == OK){
                        alert("修改成功")
                        location.href="/index.html"
                    }
                })
        }

    },
    created(){
        this.getQuestion()
        this.loadTags()
        this.loadTeachers()
    }
})